<?php
session_start();
ob_start();
include("../function.php");
condb();
$res_id=$_GET['res_id'];
$val_id=$_GET['val_id'];
$pet_res=$_GET['pet_res'];
$search=$_GET['search'];

?>
<meta charset="utf-8" />
<center>
	<form>
    	<input name="search" />
        <input type="hidden" name="res_id" value="<?php echo $res_id;?>" />
        <input type="hidden" name="val_id" value="<?php echo $val_id;?>" />
        <input type="hidden" name="pet_res" value="<?php echo $pet_res;?>" />
        <input type="submit" value="ค้นหา" />
    </form>
</center>
<?php
  #แบ่งหน้า
  $sql="select * from customer where cus_name like '%$search%' and cus_status=1 order by cus_id";
  $res=mysql_query($sql);
  $page=$_GET['page'];
  if(!$page)$page=1;
  $allrows=mysql_num_rows($res);
  $allpage=ceil($allrows/$cfg_limit);
  $limitstart=($page-1)*$cfg_limit;
  $sql.=" limit $limitstart,$cfg_limit";
  $res=mysql_query($sql);
  $pageurl="getcustomer.php?search=$search&res_id=$_GET[res_id]&val_id=$_GET[val_id]&pet_res=$_GET[pet_res]";
  #สิ้นสุดการแบ่งหน้า
  if($allrows>0){
?>
<table width="680" cellpadding="0">
  <tr bgcolor="#00ddff">
    <td align="center"><strong>ลำดับที่</strong></td>
    <td><strong>ชื่อ-สกุล</strong></td>
    <td><strong>หมายเลขโทรศัพท์</strong></td>
    <td align="center"><strong>ดำเนินการ</strong></td>
  </tr>
  <?php
  $i=0;
  while($row=mysql_fetch_assoc($res)){
	  $i++;
	  $bg=($i%2==0)?'#ccffcc':'ffffff';
	  	if($pet_res!=''){
			$respet=mysql_query("select * from pet where cus_id='$row[cus_id]'");
			$html='<select name=pet_id id=pet_id >';
			while($pet=mysql_fetch_assoc($respet)){
				$html.="<option value=$pet[pet_id]>$pet[pet_name]</option>";
			}
			$html.='</select>';
	  		$pet="window.opener.document.getElementById('$pet_res').innerHTML='$html';";
	  	}
	  	else{
			$pet='';
		}
  ?>
  <tr bgcolor="<?php echo $bg;?>">
    <td align="center"><?php echo $i;?></td>
    <td><?php echo $row['cus_name'];?></td>
    <td><?php echo $row['cus_phone'];?></td>
    <td align="center"><a href="#" onclick="window.opener.document.getElementById('<?php echo $res_id;?>').innerHTML='<?php echo $row['cus_name'];?>';window.opener.document.getElementById('<?php echo $val_id?>').value='<?php echo $row['cus_id'];?>';<?php echo $pet;?>window.close();">เลือก</a></td>
  </tr>
  <?php
  }
  ?>
</table>
  <?php
	  pagemenu($pageurl,$page,$allpage);
  }
  ?>